//
//  WSVisitorView.swift
//  WSWeiBo01
//
//  Created by 李维顺 on 16/7/12.
//  Copyright © 2016年 liweishun. All rights reserved.
//

import UIKit
import SnapKit
class WSVisitorView: UIView {
    //  定义登录或者注册执行的闭包
    var loginClosure: (()->())?
    
    //懒加载控件
    //旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    //罩层
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //中心图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息内容
    private lazy var messageLabel: UILabel = {
       let lab = UILabel()
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.textColor = UIColor.darkGrayColor()
        lab.numberOfLines = 0
        lab.font = UIFont.systemFontOfSize(14)
        return lab
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
       let button = UIButton()
        
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        //设置按钮属性
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        //设置按钮属性
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.yellowColor()
        setupUI()
        
    }

    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        maskIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
        
        
    }
    
    
    //修改访客信息
    func changeVisitorInfo(message: String? , imageName: String?) {
        if let mes = message, img = imageName {
            messageLabel.text = mes
            iconImageView.image = UIImage(named: img)
            cycleImageView.hidden = true
        }else {
            //是首页
            cycleImageView.hidden = false
            startAnimation()
        }
        
        
        
    }
    
    
    //开启动画
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //动画目的
        animation.toValue = 2 * M_PI
        //动画时间
        animation.duration = 20
        //重复次数
        animation.repeatCount = MAXFLOAT
        //失去焦点不释放
        animation.removedOnCompletion = false
        //执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
        
        
    }
    
    
    
    
    
    
    // MARK: --点击事件
    @objc private func registerAction() {
        
        print("注册点击")
        loginClosure?()
    }
    @objc private func loginAction() {
        
        print("登录点击")
        loginClosure?()
    }
    
    
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}




















